package com.dk.mq;

import cn.hutool.core.util.RandomUtil;
import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.dysmsapi20170525.models.SendSmsResponseBody;
import com.aliyun.tea.TeaException;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.Common;
import com.aliyun.teautil.models.RuntimeOptions;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
@RocketMQMessageListener(topic = "phone",consumerGroup = "phone")
public class UserPhoneConsumer implements RocketMQListener<String> {
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void onMessage(String phoneNum) {
        Client client = null;
        try {
            client = createClient("LTAI5t8egQ8mrPYi5LLEcNY7", "XtLOW5rpVbgwIGHEuCCh95sFVP2gUa");
        } catch (Exception e) {
            e.printStackTrace();
        }
        //随机生成的验证码
        String code = RandomUtil.randomNumbers(4);
        SendSmsRequest sendSmsRequest = new SendSmsRequest();
        sendSmsRequest.setPhoneNumbers(phoneNum);
        sendSmsRequest.setSignName("东科创想");
        sendSmsRequest.setTemplateCode("SMS_162522027");
        sendSmsRequest.setTemplateParam("{\"sms_code\":\""+code+"\"}");
        RuntimeOptions runtime = new RuntimeOptions();
        try {
            // 复制代码运行请自行打印 API 的返回值
            SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, runtime);
            System.out.println("短信发送状态:"+sendSmsResponse.getStatusCode()); // 相当于我们 result的 code
            //短信发送响应体.相当于我们的 result 中的 data;
            SendSmsResponseBody body = sendSmsResponse.getBody();
            System.out.println(body.getCode());
            System.out.println(body.getMessage());

            redisTemplate.opsForValue().set(phoneNum,code,30, TimeUnit.MINUTES);
        } catch (TeaException error) {
            // 如有需要，请打印 error
            Common.assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 如有需要，请打印 error
            Common.assertAsString(error.message);
        }
    }

    /**
     * 使用AK&SK初始化账号Client
     * @param accessKeyId
     * @param accessKeySecret
     * @return Client
     * @throws Exception
     */
    public Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        Config config = new Config()
                // 您的 AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 您的 AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new Client(config);
    }
}
